export default {

    namespaced: true,

    state: {
        carts: [],       // 存放购物车列表数据
        cartBuyNum: localStorage.getItem('cartBuyNum') || 0    // 存放商品数量
    },
    getters: {

    },
    mutations: {
        setCartBuyNum(state) {
            state.cartBuyNum++
            localStorage.setItem('cartBuyNum', state.cartBuyNum)
        },
        setCarts(state, data) {
            state.carts = data
        }
    },
    actions: {
        setCarts(context) {
            // 发送异步请求
            let resData = [
                {id: 1, title: "张三", price : 1.11},
                {id: 2, title: "李四", price : 2.11},
                {id: 3, title: "王五", price : 3.11},
                {id: 4, title: "赵六", price : 4.11}
            ]
            // 触发mutations -> 更新数据
            context.commit('setCarts', resData)
        }
    }
}